﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

using DotSpatial;
using DotSpatial.Data;

using System.Drawing;

using DrawContour;

namespace HuanGis
{


    






 

   public class FDContourLayer:FEditableLayer
   {

       public FDContourLayer(string id)
           : base(id)
       {

           //第一步给这个层一些基本的定义
           this.m_bSelectable = false;

           this.m_bShapeEditable = false;




           this.m_Type = DotSpatial.Geometries.FeatureTypes.Polygon;

           //初始化区域填充色
           this._LineFillColorList = new List<System.Drawing.Color>();
           this._LineFillColorList.Add(System.Drawing.Color.FromArgb(56, 168, 23));
           this._LineFillColorList.Add(System.Drawing.Color.FromArgb(255, 100, 100));
           this._LineFillColorList.Add(System.Drawing.Color.FromArgb(255, 13, 53));
           this._LineFillColorList.Add(System.Drawing.Color.FromArgb(153, 255, 204));
           //初始化等值线颜色
           this._LineColorList = new List<System.Drawing.Color>();
           this._LineColorList.Add(System.Drawing.Color.Black);

           //等值线集合初始化
           this._ContourLineList = new List<ContourLine>();

           ContourLine CL;
           
           for (int i = 0; i < 23; i++)
           {
               CL = new ContourLine();
               CL.ContourLineValue = 33 + i * 2;
               CL.ContourLineFillColor = System.Drawing.Color.FromArgb(i * 2 , i * 8 + 3, i * 2 + 2);
               this._ContourLineList.Add(CL);
           }


               //初始化其它
           this._ContourLineOffsetX = 0; //相对坐标 相对绝对坐标的偏移量
           this._ContourLineOffsetY = 0;

           this._IsLegendTextFormat = false;
           this._LegendFormatNum = 3;

           this._LineWidth = 1;

           this.MyContours = this.MyContours.Open(@"C:\Documents and Settings\Administrator\桌面\ContourLine.SSS");
      
           
       
       }


       # region 字段

       public DrawContour.Contours MyContours = new DrawContour.Contours();
       private bool IsContourReady;

       private double xScale = 1; //变化比例

       protected double _LineWidth = 1;

       //-----------------------------------------------------------------------------//

       private bool _IsShowLegend = false; //是否显示图例
       private bool _IsLegendTextFormat; //是否启用图例文本格式化

       private int _LegendFormatNum; //图例小数点位数

       private double _LegendOffsetX = 20; //图例X方向偏移量
       private double _LegendOffsetY; //图例Y方向偏移量

       private double _ContourLineOffsetX; //等值线X方向偏移量。  应该这样注释：（相对坐标 相对绝对坐标的偏移量）
       private double _ContourLineOffsetY; //等值线Y方向偏移量

       private bool _IsLineSmooth; //等值线是否需要光滑

       private Int64 _LineLableFormatNum; //等值线标注小数点位数
       private int _LineLableNum; //等值线标注采样点数

       private bool _IsLableOnLine; //标注是否在等值线上

       //------------------------------------------------------------------------------//

       private List<System.Drawing.Color> _LineFillColorList;  //填充渐变颜色集合

       private List<System.Drawing.Color> _LineColorList;    //线条渐变色集合

       private List<ContourLine> _ContourLineList;

       # endregion


       # region 属性

       /// <summary>
       /// 填充渐变颜色集合,集合形式
       /// </summary>
       [Category ("颜色渐变集合"),Browsable (false ),DisplayName ("填充渐变色集合")]
       public List<System.Drawing.Color> LineFillColorList
       {
           get { return this._LineFillColorList; }
           set { this._LineFillColorList = value; }
       }

       /// <summary>
       /// 填充渐变颜色集合,数组形式
       /// </summary>
       [Category("颜色渐变集合"),DisplayName("填充渐变色集合")]
       public System.Drawing.Color[] LineFillColorArray
       {
           get { return this._LineFillColorList.ToArray(); }
           set
           {
               if (value.Length > 0)
               {
                   this._LineFillColorList.Clear();
                   this._LineFillColorList.AddRange(value);
               }
               this.SetContourColor();
           }
       }
       



       /// <summary>
       /// 线条渐变色集合，集合形式
       /// </summary>
       [Category ("颜色渐变集合"),Browsable (false ),DisplayName ("线条渐变色集合")]
       public List<System.Drawing.Color> LineColorList
       {
           get { return this._LineColorList; }
           set { this._LineColorList = value; }
       }

       /// <summary>
       /// 线条渐变色集合,数组形式
       /// </summary>
       [Category("颜色渐变集合"), DisplayName("线条渐变色集合")]
       public System.Drawing.Color[] LineColorArray
       {
           get { return this._LineColorList.ToArray(); }
           set
           {
               if (value.Length > 0)
               {
                   this._LineColorList.Clear();
                   this._LineColorList.AddRange(value);
               }
               this.SetContourColor();
           }
       }


       /// <summary>
       /// 等值线集合编辑器,设置等值线
       /// </summary>
       [Category("等值线设置"), Editor(typeof(ContourEditor), typeof(UITypeEditor)),DisplayName ("等值线集合编辑器")]
       public List<ContourLine> ContourLineList
       {
           get { return this._ContourLineList; }
           set 
           {
               this._ContourLineList = value;

               //还需加代码……

               double[] ContourLineValue = new double[ContourLineList.Count];

               for(int i=0;i<ContourLineList.Count ;i++)
               {
                   ContourLineValue[i] = ContourLineList[i].ContourLineValue;
               }

               this.MyContours.SetContours(ContourLineValue);

               if (this._IsLineSmooth == true)
               {
                   this.MyContours.Slip = true;
                   MyContours.SetContoursSlip();
               }
               else
               {
                   this.MyContours.Slip = false;
               }
               this.Redraw();
           }
       }



       #region 图例相关的属性
       //--------------------------------------------------------------------------//




       /// <summary>
       /// 是否显示图例
       /// </summary>
       [Category ("图例设置"),DisplayName ("是否显示图例")]
       public bool IsShowLegend
       {
           get { return this._IsShowLegend; }
           set
           {
               this._IsShowLegend = value;

               //this.DrawLegend(this._IsShowLegend);
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }



       /// <summary>
       /// 是否启用图例文本格式化
       /// </summary>
       [Category("图例设置"), DisplayName("启用图例文本格式化")]
       public bool IsLegendTextFormat
       {
           get { return this._IsLegendTextFormat; }
           set
           {
               this._IsLegendTextFormat = value;
               //this.DrawLegend(this._IsShowLegend);
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }

       /// <summary>
       /// 图例文本格式化小数点位数
       /// </summary>
       [Category("图例设置"), DisplayName("图例文本格式化小数点位数")]
       public int LegendFormatNum
       {
           get { return this._LegendFormatNum; }
           set
           {
               this._LegendFormatNum = value;
               //this.DrawLegend(this._IsShowLegend);
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }

       /// <summary>
       /// 图例X方向偏移量
       /// </summary>
       [Category("图例设置"), DisplayName("图例X方向偏移量")]
       public double LegendOffsetX
       {
           get { return this._LegendOffsetX; }
           set
           {
               this._LegendOffsetX = value;
               //this.DrawLegend(this._IsShowLegend);
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }

       /// <summary>
       /// 图例Y方向偏移量
       /// </summary>
       [Category("图例设置"), DisplayName("图例Y方向偏移量")]
       public double LegendOffsetY
       {
           get { return this._LegendOffsetY; }
           set
           {
               this._LegendOffsetY = value;
               //this.DrawLegend(this._IsShowLegend);
               this.m_ParenLManager.CurrentMap.Refresh();
           }
       }

       #endregion


       /// <summary>
       /// 等值线X轴方向偏移值
       /// </summary>
       [Browsable (false)]
       public double ContourLineOffsetX
       {
           get { return this._ContourLineOffsetX; }
           set
           {
               this._ContourLineOffsetX = value;
               this.Update();
           }
       }

       /// <summary>
       /// 等值线Y轴方向偏移值
       /// </summary>
       [Browsable (false)]
       public double ContourLineOffsetY
       {
           get { return this._ContourLineOffsetY; }
           set
           {
               this._ContourLineOffsetY = value;
               this.Update ();
           }
       }

       /// <summary>
       /// 是否启用等值线光滑
       /// </summary>
       [Category ("等值线设置"),Description ("启用后等值线会插值，可能使显示速度变慢") , DisplayName ("是否启用等值线光滑")]
       public bool IsLineSmooth
       {
           get { return this._IsLineSmooth; }
           set
           {
               this._IsLineSmooth = value;
               this.OnSetSmoothLine();        //这里还要加入光滑的代码……
           }
       }

       
       /// <summary>
       /// 等值线标注格式化小数点位数
       /// </summary>
       [Category("等值线标注"), DisplayName("等值线标注格式化小数点位数")]
       public Int64 LineLableFormatNum
       {
           get { return this._LineLableFormatNum; }
           set
           {
               this._LineLableFormatNum = value;
               this.DrawLable();
           }
       }

       /// <summary>
       /// 等值线标注采样点数
       /// </summary>
       [Category("等值线标注"), DisplayName("等值线标注采样点数")]
       public int LineLableNum
       {
           get { return this._LineLableNum; }
           set
           {
               this._LineLableNum = value;
               this.DrawLable();
           }
       }

       /// <summary>
       /// 标注位置是否在线上
       /// </summary>
       [Category("等值线标注"), DisplayName("标注位置是否在线上")]
       public bool IsLableOnLine
       {
           get { return this._IsLableOnLine; }
           set
           {
               this._IsLableOnLine = value;
               this.DrawLable();
           }
       }

       /// <summary>
       /// 是否显示等值线标注
       /// </summary>
       [Category ("等值线标注"),DisplayName ("是否显示等值线标注")]
       public bool IsShowLable
       {
           get { return this.m_IsShowLable; }
           set 
           {
               this.m_IsShowLable = value;
               this.DrawLable();
           }
       }

       /// <summary>
       /// 等值线线条宽度
       /// </summary>
       [Category ("外观设置"),DisplayName ("线条宽度")]
       public double LineWidth
       {
           get { return this._LineWidth; }
           set 
           {
               this._LineWidth = value;
               this.SetLineWidth();
           }
       }


       # endregion


       # region 方法

       /// <summary>
       /// 
       /// </summary>
       /// <param name="Contours"></param>
       public void SetContours(DrawContour.Contours Contours)
       {
           this.MyContours = Contours;
       }


       /// <summary>
       /// 画等值线
       /// </summary>
       public void DrawContourLines()
       {
           if (this.ContourLineList == null || this.ContourLineList.Count == 0) return; //不为空

           if (this.MyContours.GridPoint.Length <= 0) return;

           //排序，去重
           System.Comparison<ContourLine> a = new Comparison<ContourLine>(CompareContour);
           this.ContourLineList.Sort(a);

           List<ContourLine> tempLineList = new List<ContourLine>();
           foreach (ContourLine cl in this.ContourLineList)
           {
               if (tempLineList.Contains(cl) == false)
                   tempLineList.Add(cl);
           }

           //下面是自我挖空，生成图形（同一个等值线值可能存在N条等值线，这些等值线有可能相离、有可能是包含）
           Dictionary<double, Feature[]> DFeatures = new Dictionary<double, Feature[]>();
           List<Feature> Features;
           
           foreach (ContourLine cl in tempLineList)
           {
               //挖空图形，生成图形
               Features = this.GetOriginPolygonWithOneVal(cl.ContourLineValue); //一个等值线值对应的所有等值线生成的图形
               if (Features != null)
               {
                   DFeatures.Add(cl.ContourLineValue, Features.ToArray());
               }
               else
               {
                   //这样写不知道对不对！！！！！！
                   Feature[] tempFeatures = null;
                   DFeatures.Add(cl.ContourLineValue, tempFeatures);
               }
           }

           //==================================================================//
           //下面是对不同等值线的值生成的图形进行比较、挖空等，生成新的图形
                      
           Dictionary<double, Feature[]> HoleFeatures = new Dictionary<double, Feature[]>();

           for (int i = tempLineList.Count - 1; i >= 0; i--)
           {
               if (i == tempLineList.Count - 1)
               {
                   if (DFeatures.ContainsKey(tempLineList[i].ContourLineValue))
                   {
                       Feature[] F = DFeatures[tempLineList[i].ContourLineValue];
                       HoleFeatures.Add(tempLineList[i].ContourLineValue, F);
                   }
               }
               else
               {
                   //等值线值大的等值线包含的面Shp, 包括的范围小
                   Feature[] BigValFeatures = DFeatures[tempLineList[i + 1].ContourLineValue];
                   //等值线值小的等值线包含的面Shp, 包括的范围大
                   Feature[] SmallValFeatures = DFeatures[tempLineList[i].ContourLineValue];
                   //范围大的减去范围小的就是两条等值线包含的区域
                   List<Feature> resultFeatures = new List<Feature>();

                   //外层循环大的图形（小的等值线值构成的图形）
                   foreach (Feature SmallValFeature in SmallValFeatures)
                   {
                       DotSpatial.Geometries.LinearRing Shell = new DotSpatial.Geometries.LinearRing(SmallValFeature.Coordinates.ToArray());
                      
                       Feature tempfeature = new Feature(SmallValFeature.BasicGeometry);
                                              
                       //内层循环小的图形（大的等值线值构成的图形）
                       foreach (Feature BigValFeature in BigValFeatures)
                       {
                           if (tempfeature.Contains(BigValFeature))
                           {
                               tempfeature = tempfeature.Difference(BigValFeature) as Feature;
                           }
                                                      
                       }
                      
                       resultFeatures.Add(tempfeature);
                                             
                   }
                   HoleFeatures.Add(tempLineList[i].ContourLineValue, resultFeatures.ToArray());                   
 
               }
           }

           //所有的图形生成完毕……
           //===============================================================================================//
           //以下是绘制以及添色等

           //添图形
           if ((this.ILayer.DataSet as FeatureSet).DataTable.Columns.Contains("Value") == false)
               (this.ILayer.DataSet as FeatureSet).DataTable.Columns.Add("Value", typeof(double));  //设置字段名称


           foreach (ContourLine CL in tempLineList)
           {               
               foreach (Feature f in HoleFeatures[CL.ContourLineValue])
               {

                   //本来在这个循环内设置颜色更加有效率，但是因为在外部还要设置颜色，
                   //所以在外部写了一个函数，方便其它调用

                   (this.ILayer.DataSet as FeatureSet).Features.Add(f); //添加图形
                   f.DataRow["Value"] = CL.ContourLineValue;

               }               
              
           }
                      
          
           //清空与重新赋值
           this._ContourLineList.Clear();
           this._ContourLineList.AddRange(tempLineList.ToArray());

           //颜色设置

           foreach (ContourLine CL in this._ContourLineList)
           {
               this.SetContourColor(CL.ContourLineValue, CL.ContourLineFillColor, CL.ContourLineColor, true);
           }

           //this.SetContourColor();

           //保存等值线层，测试用……   

           (this.ILayer.DataSet as FeatureSet).SaveAs(@"c:/1.shp", true);

       }


       /// <summary>
       /// 设置等值线填充色与线颜色
       /// 这个主要是根据颜色集合来绘制……
       /// </summary>
       private void SetContourColor()
       {
           //这里一定要清空，不然严重影响速度，下面会添加新的Category
           (this.ILayer as DotSpatial.Drawing.IFeatureLayer).Symbology.ClearCategories();

           List<System.Drawing.Color> ContourLineColorList = new List<System.Drawing.Color>();
           List<System.Drawing.Color> ContourLineFillColorList = new List<System.Drawing.Color>();

           ContourLineColorList = MyArithmetic.BuildColor(this._LineColorList);
           ContourLineFillColorList = MyArithmetic.BuildColor(this._LineFillColorList);

           for (int i = 0; i < this._ContourLineList.Count; i++)
           {
               this._ContourLineList[i].ContourLineColor = ContourLineColorList[MyArithmetic.GetColorPos(ContourLineColorList.Count, i, this._ContourLineList.Count,
                   this._ContourLineList[0].ContourLineValue, this._ContourLineList[this._ContourLineList.Count - 1].ContourLineValue)];
               this._ContourLineList[i].ContourLineFillColor = ContourLineFillColorList[MyArithmetic.GetColorPos(ContourLineFillColorList.Count, i, this._ContourLineList.Count,
                   this._ContourLineList[0].ContourLineValue, this._ContourLineList[this._ContourLineList.Count - 1].ContourLineValue)];

               //下面这两句是定义类型（一个feature的颜色、添充色、outline宽等）
               DotSpatial.Drawing.PolygonCategory pc = new DotSpatial.Drawing.PolygonCategory(this._ContourLineList[i].ContourLineFillColor, this._ContourLineList[i].ContourLineColor, this._LineWidth);
               //添加新的Category
               (this.ILayer as DotSpatial.Drawing.IFeatureLayer).Symbology.AddCategory(pc);

               foreach (Feature f in (this.ILayer.DataSet as FeatureSet).Features)
               {
                   if ((double)f.DataRow["Value"] == this._ContourLineList[i].ContourLineValue)
                   {
                       (this.ILayer as DotSpatial.Drawing.IFeatureLayer).SetCategory(f, pc);
                   }
               }

           }

       }



       /// <summary>
       /// 一个自定义排序方法，排序等值线（根据等值线值）
       /// </summary>
       /// <param name="X"></param>
       /// <param name="Y"></param>
       /// <returns></returns>
       private static int CompareContour(ContourLine X, ContourLine Y)
       {
           if (X == null)
           {
               if (Y == null)
               {
                   return 0;
               }
               else
               {
                   return -1;
               }
           
           }
           else
           {
               if (Y == null)
               {
                   return 1;
               }
               else
               {
                   int val;
                   val = X.ContourLineValue.CompareTo(Y.ContourLineValue);
                   return val;
               }
           }

       }


       /// <summary>
       /// 根据一个等值线值获得这个等值线值对应的所有等值线生成的图形（面图形）
       /// </summary>
       /// <param name="ContourLineVal">一个等值线的值</param>
       /// <returns></returns>
       protected List<Feature> GetOriginPolygonWithOneVal(double ContourLineVal)
       {
           List<Feature> Features = new List<Feature>();

           if (this.MyContours == null) return null;


           //同一个等值线值可能存在N条等值线，这些等值线有可能相离、有可能是包含
           DrawContour.ContourPoints[] ContourLines;
           ContourLines = MyContours.GetContourLines(ContourLineVal);

           //double a = this.MyContours.ArrayContour[0].ContourValue;

           if (ContourLines == null || ContourLines.Length <= 0)
               return null;


           List<DotSpatial.Geometries.Coordinate[]> ClipPoints = new List<DotSpatial.Geometries.Coordinate[]>();

           //循环每一条等值线,找出所有的洞存储……
           foreach (DrawContour.ContourPoints ptLine in ContourLines)
           {
               //如果等值线区域内的值小于等值线对应的值（这里的值是指：比如噪声值），
               //则说明这条等值线属于等值线区域内嵌的等值线
               if (ptLine.Great == 1)
               {
                   //int N = ptLine.vecContourPoints.Length;
                   //我还是用集合吧，C#与VB的数组太容易混淆了……

                   List<DotSpatial.Geometries.Coordinate> ptList = new List<DotSpatial.Geometries.Coordinate>();
                   foreach (DrawContour.Point3D Pt3D in ptLine.vecContourPoints)
                   {
                       DotSpatial.Geometries.Coordinate C = new DotSpatial.Geometries.Coordinate();
                       C.X = Pt3D.x + this._ContourLineOffsetX; //需转换成绝对坐标
                       C.Y = Pt3D.y + this._ContourLineOffsetY;
                       ptList.Add(C);
                   }
                   ClipPoints.Add(ptList.ToArray());

               }

           }

           //再循环每条线，找出每条外边的线，每条边线与几个内线（洞）生成一个Shp……
           foreach (DrawContour.ContourPoints ptLine in ContourLines)
           {
               //判断：如果是边线，生成Shp
               if (ptLine.Great == 0) //这里与付的不同，我认为这样更有效率……
               {
                   List<DotSpatial.Geometries.Coordinate> ptList = new List<DotSpatial.Geometries.Coordinate>();
                   foreach (DrawContour.Point3D Pt3D in ptLine.vecContourPoints)
                   {
                       DotSpatial.Geometries.Coordinate C = new DotSpatial.Geometries.Coordinate();
                       C.X = Pt3D.x + this._ContourLineOffsetX; //需转换成绝对坐标
                       C.Y = Pt3D.y + this._ContourLineOffsetY;
                       ptList.Add(C);
                   }
                   //生成Shp
                   Feature F = this.GetShapePolygonWithHole(ptList.ToArray(), ClipPoints);
                   Features.Add(F);
               
               }
           }
           
           return Features;

       }


       /// <summary>
       /// 给定一条边线，N条内线（洞）,求出哪条内线（洞）在边线内并生成Shp(生成的可能是1个或N个图形)
       /// </summary>
       /// <param name="ptList">一条边线的点</param>
       /// <param name="ClipPoints">N条内线（洞）</param>
       /// <returns></returns>
       private Feature GetShapePolygonWithHole(DotSpatial.Geometries.Coordinate[] ptList, List<DotSpatial.Geometries.Coordinate[]> ClipPoints)
       {
           Feature F;
           DotSpatial.Geometries.Polygon Polygon;


           //外边线
           DotSpatial.Geometries.LinearRing shell = new DotSpatial.Geometries.LinearRing(ptList.ToArray());
           DotSpatial.Geometries.Polygon shellPolygon = new DotSpatial.Geometries.Polygon(ptList.ToArray());
           //洞
           List<DotSpatial.Geometries.LinearRing > holes=new List<DotSpatial.Geometries.LinearRing> ();

           foreach (DotSpatial.Geometries.Coordinate[] pts in ClipPoints)
           {
               //要注意反转坐标，hole的坐标与shell的坐标方向必须不同，一个是顺时针，一个是逆时针……
               DotSpatial.Geometries.LinearRing hole = new DotSpatial.Geometries.LinearRing(pts.Reverse());
               DotSpatial.Geometries.Polygon holePolygon = new DotSpatial.Geometries.Polygon(pts);

               //同样是Geometry类型,像这样用就不行……
               //if (Shell.Contains(hole)) 
               //{
               //holes.Add(hole);
               //}

               //必须先生成Polygon，才能用Contains方法…… 无语中……
               //if (ShellPolygon.Contains(holePolygon))
               //{
               //    holes.Add(hole);
               //}


               //如果洞在外边线内
               if (shellPolygon.Contains(holePolygon))  //Contains与Covers方法有什么不同呢？？？？？
                   holes.Add(hole); //把符合条件的洞存储起来
               
           }
           //生成多边形
           if (holes.Count > 0)
               Polygon = new DotSpatial.Geometries.Polygon(shell, holes.ToArray());
           else
               Polygon = new DotSpatial.Geometries.Polygon(shell);
           F = new Feature(Polygon);

           return F;
       }



       /// <summary>
       /// 设置单个等值线填充色与线颜色
       /// 这个主要是根据等值线编辑器来绘制,根据每个等值线的属性设置
       /// </summary>
       /// <param name="ContourLineVal">等值线值</param>
       /// <param name="FillColor">填充色</param>
       /// <param name="LineColor">线颜色</param>
       /// <param name="IsShowPolygon"></param>
       protected void SetContourColor(double ContourLineVal, System.Drawing.Color FillColor, System.Drawing.Color LineColor, bool IsShowPolygon)
       {
           DotSpatial.Drawing.PolygonCategory pc = new DotSpatial.Drawing.PolygonCategory(FillColor, LineColor, this._LineWidth);
           (this.ILayer as DotSpatial.Drawing.IFeatureLayer).Symbology.AddCategory(pc); //这一句很重要，害我搞了一天……
           foreach (Feature f in (this.ILayer.DataSet as FeatureSet).Features)
           {
               if ((double)f.DataRow["Value"] == ContourLineVal)
               {
                   (this.ILayer as DotSpatial.Drawing.IFeatureLayer).SetCategory(f, pc);  //设置颜色属性
               }
           }
           
       }



       protected  override void Redraw()
       {
           //base.Redraw();
           //生成等值线
           this.DrawContourLines();


           //等值线生成后才能显示图例……
           this._IsShowLegend = true;

           //map控件更新
           this.m_ParenLManager.CurrentMap.Refresh();
       }
      
        
       protected  override void BuildLayer()
       {
           base.BuildLayer();
           //利用map控件的Paint事件和GDI+画图例
           this.m_ParenLManager.CurrentMap.Paint += new PaintEventHandler(this.OnPaint);
           
       }


       protected override void BuildProp()
       {
           base.BuildProp();





       }



       /// <summary>
       /// 用于绘制图例的函数
       /// </summary>
       /// <param name="g"></param>
       private void DrawLegend(Graphics  g)
       {
           //注意把（自定义的）相对坐标转换成UTM坐标
           double X1 = this.MyContours.Xmax + this._ContourLineOffsetX + this._LegendOffsetX;
           double X2 = X1 + 20;

           double StepY = (this.MyContours.Ymax - this.MyContours.Ymin) / this.ContourLineList.Count;

           for (int i = 0; i < this.ContourLineList.Count; i++)
           {
               //一个图例由四个点组成
               DotSpatial.Geometries.Coordinate P1 = new DotSpatial.Geometries.Coordinate();
               DotSpatial.Geometries.Coordinate P2 = new DotSpatial.Geometries.Coordinate();
               DotSpatial.Geometries.Coordinate P3 = new DotSpatial.Geometries.Coordinate();
               DotSpatial.Geometries.Coordinate P4 = new DotSpatial.Geometries.Coordinate();

               P1.X = X1;
               P1.Y = this.MyContours.Ymin + this._ContourLineOffsetY + this._LegendOffsetY + i * StepY;
               P2.X = X2;
               P2.Y = P1.Y;
               P3.X = X2;
               P3.Y = P1.Y + StepY;
               P4.X = P1.X;
               P4.Y = P3.Y;

               //再把UTM坐标转换成系统坐标
               List<System.Drawing.PointF> PtList = new List<System.Drawing.PointF>();
               PtList.Add(this.m_ParenLManager.CurrentMap.ProjToPixel(P1));
               PtList.Add(this.m_ParenLManager.CurrentMap.ProjToPixel(P2));
               PtList.Add(this.m_ParenLManager.CurrentMap.ProjToPixel(P3));
               PtList.Add(this.m_ParenLManager.CurrentMap.ProjToPixel(P4));

               System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(this.ContourLineList[i].ContourLineFillColor);
               //画图例
               g.FillPolygon(brush, PtList.ToArray());
               //边框
               g.DrawPolygon(System.Drawing.Pens.Black, PtList.ToArray());
               //画字符串
               if (this._IsLegendTextFormat == true)  //启用图例文本格式化
                   g.DrawString(Math.Round(this.ContourLineList[i].ContourLineValue, this._LegendFormatNum).ToString(), new System.Drawing.Font("Times New Roman", 12),
                       System.Drawing.Brushes.Red, this.m_ParenLManager.CurrentMap.ProjToPixel(P2).X, this.m_ParenLManager.CurrentMap.ProjToPixel(P2).Y - 20);
               else
                   g.DrawString(this.ContourLineList[i].ContourLineValue.ToString(), new System.Drawing.Font("Times New Roman", 12),
                   System.Drawing.Brushes.Red, this.m_ParenLManager.CurrentMap.ProjToPixel(P2).X, this.m_ParenLManager.CurrentMap.ProjToPixel(P2).Y - 20);


           }
 

       }


       /// <summary>
       /// 画图例
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void OnPaint(object sender, PaintEventArgs e)
       {
           //如果不显示图例，返回，不绘制……
           if (this.IsShowLegend == false)
           {

           }
           else
           {
               this.DrawLegend(e.Graphics );
              

           }


           
       }



       /// <summary>
       /// 等值线光滑
       /// </summary>
       protected void OnSetSmoothLine()
       {
           this.MyContours.Slip = this._IsLineSmooth;

           if (this.MyContours.Slip)
           {
               MyContours.SetContoursSlip();
           }

           this.Redraw();
       }

       /// <summary>
       /// 画标注
       /// </summary>
       protected void DrawLable()
       { 
       }


       /// <summary>
       /// 设置等值线的线宽！实际设置的是面层的OutLine
       /// </summary>
       private void SetLineWidth()
       {
           //为什么这一句在这就是不行呢？？？？？？？？？？？？？
           //(this.ILayer as DotSpatial.Drawing.IFeatureLayer).Symbolizer.SetOutline(System.Drawing.Color.Black, this._LineWidth);

           //害我一个一个的设置吧……
           foreach (Feature f in (this.ILayer.DataSet as FeatureSet).Features)
           {
               DotSpatial.Drawing.PolygonCategory OldCategory = (this.ILayer as DotSpatial.Drawing.IFeatureLayer).GetCategory(f) as DotSpatial.Drawing.PolygonCategory;
               //OldCategory.Symbolizer.SetOutline(OldCategory.Symbolizer.GetFillColor(), this._LineWidth);
               OldCategory.Symbolizer.SetOutlineWidth(this._LineWidth);

           }

           this.m_ParenLManager.CurrentMap.MapFrame.Initialize();

           //this.m_ParenLManager.CurrentMap.Refresh();

       }


       # endregion


   }
}
